package com.fighthouse.service;

import java.util.List;
import java.util.TreeMap;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

import org.apache.log4j.Logger;

import com.fighthouse.entity.Atleta;
import com.fighthouse.entity.Idioma;
import com.fighthouse.entity.Item;


public class MenuItemService {
	
	private static final String PERSISTENCE_UNIT = "fight_site";
	private static Logger logger = Logger.getLogger(Item.class);
	private static Logger loggerIdioma = Logger.getLogger(Idioma.class);
	
	public MenuItemService(){
		super();
	}
	
	@SuppressWarnings("unchecked")
	public List<Item> getList(){
		
		logger.debug("** Chamada aos itens de menu **");
		logger.debug("** Abrindo a conexão **");
		EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
		EntityManager em = entityManagerFactory.createEntityManager();
		
		//Busca por named query e hql		
		Query query = (Query) em.createNamedQuery("menuitens.getMenuItens").setParameter("tipo", 2);
		List<Item> menu = (List<Item>) query.getResultList();
		
		//Ordenação do submenu
		for(Item item:menu){
			if(item.getSubmenu() != null){
				TreeMap<Item,Integer> ordenada = new TreeMap();
				for(Item itemSub:item.getSubmenu())ordenada.put(itemSub, itemSub.getId());
				item.setSubmenu(ordenada.keySet());
			}
		}
		
		
		if(menu != null)logger.debug("** Encontrados " + menu.size() + " itens");
		
		logger.debug("** Fechando a conexão **");
		em.close();
		entityManagerFactory.close();
		
		return menu;
		
	}
	
	@SuppressWarnings("unchecked")
	public List<Item> getInvisibleList(){
		logger.debug("** Chamada aos itens de menu Invisíveis**");
		logger.debug("** Abrindo a conexão **");
		EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
		EntityManager em = entityManagerFactory.createEntityManager();
		
		//Busca por named query e hql		
		Query query = (Query) em.createNamedQuery("menuitens.getMenuItensInvisible").setParameter("tipo", 2);
		List<Item> menu = (List<Item>) query.getResultList();
		
		//Ordenação do submenu
		for(Item item:menu){
			if(item.getSubmenu() != null){
				TreeMap<Item,Integer> ordenada = new TreeMap();
				for(Item itemSub:item.getSubmenu())ordenada.put(itemSub, itemSub.getId());
				item.setSubmenu(ordenada.keySet());
			}
		}
		
		
		if(menu != null)logger.debug("** Encontrados " + menu.size() + " itens");
		
		logger.debug("** Fechando a conexão **");
		em.close();
		entityManagerFactory.close();
		
		return menu;
	}
	
	@SuppressWarnings("unchecked")
	public List<Idioma> getListIdioma(){
		loggerIdioma.debug("** Chamada aos idiomas **");
		loggerIdioma.debug("** Abrindo a conexão **");
		EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
		EntityManager em = entityManagerFactory.createEntityManager();
		

		List<Idioma> menu = (List<Idioma>) em.createNamedQuery("idiomas.getIdiomas").getResultList();
		
		if(menu != null)logger.debug("** Encontrados " + menu.size() + " itens");
		
		loggerIdioma.debug("** Fechando a conexão **");
		em.close();
		entityManagerFactory.close();
		
		return menu;
	}
	public Item update(Item item){
		logger.debug("** update item **");
		logger.debug("** Abrindo a conexão **");
		EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
		EntityManager em = entityManagerFactory.createEntityManager();

		EntityTransaction tx = em.getTransaction();
		
		tx.begin();
		
		try{
			item = em.merge(item);
			tx.commit();
		}catch(Exception e){
			tx.rollback();
			System.out.println(e.getMessage());
		}
		
		logger.debug("** Fechando a conexão **");
		em.close();
		entityManagerFactory.close();
		
		return item;
	}
	
}
